package org.example.traffic.videoInfo;

import org.example.traffic.videoInfo.Video;
import org.example.traffic.videoInfo.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author: wyk
 */
@RestController
@RequestMapping("/videos")
public class VideoController {

    @Autowired
    private VideoService videoService;
    //上次视频

    @PostMapping("/upload")
    public ResponseEntity<Video> uploadVideo(@RequestParam("file") MultipartFile file) {
        try {
            Video video = videoService.store(file);
            return ResponseEntity.status(HttpStatus.OK).body(video);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(null);
        }
    }
    // 分页查询视频
    @GetMapping
    public ResponseEntity<Page<Video>> getVideos(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size) {
        Page<Video> videos = videoService.getVideos(page-1, size);
        return ResponseEntity.ok(videos);
    }

}
